'''
封装Requests中的get、post方法，目的
1.增加异常处理
2.使用Session发请求，自动管理cookie
'''
import requests

class BaseRequests:
    def __init__(self):
        # 定义一个session属性
        self.session = requests.session()

    def get(self,url,**kwargs):
        try:
            r = self.session.get(url,**kwargs)
            print(f'发送get请求失败,url:{url}, 参数:{kwargs}')
            return r
        except Exception as e:
            print(f'发送get请求失败,url:{url}, 参数:{kwargs},异常信息:{e}')

    def post(self,url,**kwargs):
        try:
            r = self.session.post(url,**kwargs)
            print(f'发送post请求成功,url:{url},参数:{kwargs}')
            return r
        except Exception as e:
            print(f'发送post请求成功,url:{url},参数:{kwargs},异常信息为:{e}')

# 测试代码，用完删除
if __name__ == '__main__':
    url = 'http://192.168.2.36:8089/futureloan/mvc/api/member/register'
    cs = {'mobilephone':'18012345678','pwd':'123'}
    r = BaseRequests().get(url,params=cs)
    print(r.json())
    r = BaseRequests().post(url,data=cs)
    print(r.json())